package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.entity.VipTokens;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.mapper.VipOrdersMapper;
import com.robotserver.orders.mapper.VipTokensMapper;
import com.robotserver.orders.param.PddOrderSyncParam;
import com.robotserver.orders.param.biz.VipOrderPullParam;
import com.robotserver.orders.param.VipOrderSyncParam;
import com.robotserver.orders.service.VipOrderService;
import com.robotserver.orders.service.VipOrderSyncService;
import com.robotserver.orders.service.VipRefundOrderService;
import com.robotserver.orders.vo.VipOrderTimeVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: jianghan
 * @Date: 2019/11/14 09:42
 * @Description:
 */
@RestController
public class VipOrderController {

    private static Logger logger = LoggerFactory.getLogger(VipOrderController.class);

    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private VipOrderSyncService vipOrderSyncService;
    @Autowired
    private VipOrdersMapper vipOrdersMapper;
    @Autowired
    private VipRefundOrderService vipRefundOrderService;
    @Autowired
    private VipTokensMapper vipTokensMapper;

    /**
     * 本地下载
     * @param param
     * @return
     */
    @PostMapping("viporder/pull")
    public Object pullOrder(VipOrderPullParam param){
        return vipOrderService.getOrderList(param);
    }

    /**
     * 远程api下载
     * @param vipOrderSyncParam
     * @return
     */
    @PostMapping("viporder/remotePull")
    public Object remotePull(VipOrderSyncParam vipOrderSyncParam){
        vipOrderService.syncOrder(vipOrderSyncParam);
        return true;
    }

    /**
     * 远程api下载-v3版本
     * @param vipOrderSyncParam
     * @return
     */
    @PostMapping("viporder/remotePullV3")
    public Object remotePullV3(VipOrderSyncParam vipOrderSyncParam){

        logger.info("--3.0手动远程下载唯品会订单--tbOrderRefundSyncParam={}", JSONObject.toJSONString(vipOrderSyncParam));

        //判断是否订单号为空，如果不为空，则先修改数据库此订单的最新修改时间
        if(StringUtils.isNotBlank(vipOrderSyncParam.getChildOrderNo())){
            vipOrdersMapper.updateUpdateTime(vipOrderSyncParam.getChildOrderNo());
        }

        if (StringUtils.isBlank(vipOrderSyncParam.getAccessToken())
                || vipOrderSyncParam.getBegin_time() == null
                || vipOrderSyncParam.getEnd_time() == null
                || vipOrderSyncParam.getDownTimeType() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }


        //查询唯品会联盟
        VipTokens vipTokens = vipTokensMapper.getByAccessToken(vipOrderSyncParam.getAccessToken());
        if(vipTokens == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "未找到此联盟token");
        }

        //封装分段时间查询参数
        List<VipOrderTimeVO> vipOrderTimeVOS = DateUtil.getVipOrderTimeScope(vipOrderSyncParam.getBegin_time()*1000L, vipOrderSyncParam.getEnd_time()*1000L);

        vipOrderSyncService.threadSyncOrder_consumer(vipOrderTimeVOS, vipTokens, vipOrderSyncParam.getDownTimeType());
        return true;
    }


    /**
     * 远程补拉全部联盟订单-3.0V
     * @param vipOrderSyncParam
     * @return
     */
    @PostMapping("viporder/pullOrderByAllToken")
    public Object pullOrderByAllToken(VipOrderSyncParam vipOrderSyncParam){

        logger.info("--god平台手动远程下载唯品会订单--vipOrderSyncParam={}", JSONObject.toJSONString(vipOrderSyncParam));

        if (vipOrderSyncParam.getBegin_time() == null
                || vipOrderSyncParam.getEnd_time() == null
                || vipOrderSyncParam.getDownTimeType() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //普通订单
        VipOrderSyncParam vipOrderSyncParam_nomal = new VipOrderSyncParam();
        vipOrderSyncParam_nomal.setBegin_time(vipOrderSyncParam.getBegin_time() * 1000);
        vipOrderSyncParam_nomal.setEnd_time(vipOrderSyncParam.getEnd_time() * 1000);
        vipOrderSyncParam_nomal.setRedisQueueKey(CacheKey.vipPayOrderQueue);
        vipOrderSyncParam_nomal.setDownTimeType(vipOrderSyncParam.getDownTimeType());
        //调用生产者线程，遍历唯品会联盟，往redis队列中添加vipTokens
        vipOrderService.syncOrder_producer(vipOrderSyncParam_nomal);

        //维权订单
        VipOrderSyncParam vipOrderSyncParam_Refund = new VipOrderSyncParam();
        vipOrderSyncParam_Refund.setBegin_time(vipOrderSyncParam.getBegin_time() * 1000);
        vipOrderSyncParam_Refund.setEnd_time(vipOrderSyncParam.getEnd_time() * 1000);
        vipOrderSyncParam_Refund.setDownTimeType(vipOrderSyncParam.getDownTimeType());
        vipOrderSyncParam_Refund.setRedisQueueKey(CacheKey.vipRefundOrderQueue);
        vipRefundOrderService.syncOrder_producer(vipOrderSyncParam_Refund);

        return true;
    }
}
